﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class Provider
    {
        public Provider()
        { }

        public int ProviderID
        { get; set; }

        public string LastName
        { get; set; }

        public string FirstName
        { get; set; }

        public string MiddleName
        { get; set; }

        public string ContactPhone
        { get; set; }

        public string Email
        { get; set; }

        public string SpecialtyName
        { get { return Specialty.SpecialtyName; } }

        public Specialty Specialty
        { get; set; }

        public Address Address
        { get; set; }

        public string FullAddress
        {
            get
            {
                return Address.FullAddress;
            }
        }

        public string FullName
        {
            get 
            {
                return LastName + " " + FirstName + (MiddleName == null ? "" : " " + MiddleName);
            }
        }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateProvider");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@lname", SqlDbType.VarChar, 50).Value = LastName;
            command.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = FirstName;
            command.Parameters.Add("@mname", SqlDbType.VarChar, 50).Value = MiddleName;
            command.Parameters.Add("@contact_phone", SqlDbType.VarChar, 20).Value = ContactPhone;
            command.Parameters.Add("@email", SqlDbType.VarChar, 100).Value = Email;
            command.Parameters.Add("@address_id", SqlDbType.Int).Value = Address.AddressID;
            command.Parameters.Add("@specialty_id", SqlDbType.Int).Value = Specialty.SpecialtyID;
            command.Parameters.Add("@provider_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
            this.ProviderID = (int)command.Parameters["@provider_id"].Value;
        }

        public void Update()
        {
            SqlCommand command = new SqlCommand("UpdateProvider");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@lname", SqlDbType.VarChar, 50).Value = LastName;
            command.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = FirstName;
            command.Parameters.Add("@mname", SqlDbType.VarChar, 50).Value = MiddleName;
            command.Parameters.Add("@contact_phone", SqlDbType.VarChar, 20).Value = ContactPhone;
            command.Parameters.Add("@email", SqlDbType.VarChar, 100).Value = Email;
            command.Parameters.Add("@specialty_id", SqlDbType.Int).Value = Specialty.SpecialtyID;
            command.Parameters.Add("@provider_id", SqlDbType.Int).Value = ProviderID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public void Delete()
        {
            SqlCommand command = new SqlCommand("DeleteProvider");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@provider_id", SqlDbType.Int).Value = ProviderID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public static List<Provider> GetAllProviders()
        {
            DataTable table = new DataTable();
            List<Provider> objects = new List<Provider>();
            SqlCommand command = new SqlCommand("GetProviders");
            command.CommandType = CommandType.StoredProcedure;

            table = SQLDBLayer.ExecSPTable(command);

            foreach (DataRow row in table.Rows)
            {
                Provider p = new Provider();
                p.ProviderID = (int)row["provider_id"];
                p.LastName = row["lname"].ToString();
                p.FirstName = row["fname"].ToString();
                p.MiddleName = row["mname"].ToString();
                p.Email = row["email"].ToString();
                p.ContactPhone = row["contact_phone"].ToString();
               
                int address_id = (int)row["address_id"];
                int specialty_id = (int)row["specialty_id"];
                p.Address = (Address)Address.GetAllAddresses().Where(o => o.AddressID == address_id).ToList()[0];
                p.Specialty = (Specialty)Specialty.GetAllSpecialties().Where(o => o.SpecialtyID == specialty_id).ToList()[0];

                objects.Add(p);
            }

            return objects;
        }
    }
}
